package com.migraine.config;

import com.migraine.MyRealm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    @Bean
    public MyRealm myRealm() {
        return new MyRealm();
    }

    @Bean(name = "securityManager")
    public  DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("myRealm") MyRealm myRealm){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(myRealm);
        return defaultWebSecurityManager;
    }


    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("securityManager") DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        //设置安全管理器
        bean.setSecurityManager(defaultWebSecurityManager);

        Map<String,String> filterMap=new LinkedHashMap<>();

        filterMap.put("/logout", "logout");

        filterMap.put("/login","anon");
        filterMap.put("css/**","anon");
        filterMap.put("js/**","anon");
        filterMap.put("img/**","anon");
        filterMap.put("MDB-Pro/**","anon");
        filterMap.put("node_modules/**","anon");

        filterMap.put("/views/homepage/**","anon");
        filterMap.put("/views/login.html","anon");
        filterMap.put("/index/**","authc");
        filterMap.put("/personalPage/**","authc");
        filterMap.put("/room/**","authc");
        filterMap.put("/views/**","authc");

        bean.setFilterChainDefinitionMap(filterMap);
        bean.setLoginUrl("/login.html");
        return bean;
    }
}
